package cn.zmit.kuxi.holder;

import com.xdroid.functions.holder.ViewHolderBase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.JoinRecordDoingEntity;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;

/** * @author chenxu @parameter * @return */
public class JoinRecordDoingHolder extends
		ViewHolderBase<JoinRecordDoingEntity> {
	private ImageView goodsPic;
	private TextView title, goodsName, joinNeed, joinCount, joinNum;
	private ProgressBar progressBar;
	private Context context;
	private LinearLayout llRoot;
	private static OnListItemSelectListener<JoinRecordDoingEntity> onlistItemSelect;
	private TextView tv_goods_allneed;

	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_join_doing, null);
		goodsPic = (ImageView) view.findViewById(R.id.imag_goodsPic);
		title = (TextView) view.findViewById(R.id.tv_goods_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goods_detail);
		tv_goods_allneed = (TextView) view.findViewById(R.id.tv_goods_allneed);
		progressBar = (ProgressBar) view.findViewById(R.id.progressBar_goods);
		joinNeed = (TextView) view.findViewById(R.id.tv_goods_join);
		joinCount = (TextView) view.findViewById(R.id.tv_join_num);
		llRoot = (LinearLayout) view.findViewById(R.id.llRoot);
		return view;
	}

	@Override
	public void showData(final int position,
			final JoinRecordDoingEntity itemData) {
		// TODO Auto-generated method stub
		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGE_PICTURES + itemData.getUrl(), goodsPic, 0);
		} else {
			goodsPic.setImageResource(R.drawable.ic_launcher);
		}
		title.setText("第" + itemData.getTitle() + "期");
		goodsName.setText(itemData.getGoodsName());
		tv_goods_allneed.setText("总需人数：" + itemData.getJoinNeed());
		joinNeed.setText("已参与：" + itemData.getJoinCount());
		joinCount.setText("本期参与：" + itemData.getMyJoinCount() + "次");
		double all = Integer.parseInt(itemData.getJoinNeed());
		double join = Integer.parseInt(itemData.getJoinCount());
		double result = (join / all) * 100;
		progressBar.setMax(100);
		progressBar.setProgress((int) result);
		llRoot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onlistItemSelect != null) {
					onlistItemSelect.onlistItemSelect(position, itemData);
				}
			}
		});
	}

	// 设置回调
	public static void setOnListItemSelectListener(
			OnListItemSelectListener<JoinRecordDoingEntity> onListItemSelectListener) {
		JoinRecordDoingHolder.onlistItemSelect = onListItemSelectListener;
	}
}
